package leetcode.Y2024M6.study;

import org.junit.Test;

import java.util.Arrays;

public class S200 {
    public int numIslands(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }

        return count;
    }

    private void dfs(char[][] grid, int ud, int lr) {
        if (!overRange(grid, ud, lr)) return;
        if (grid[ud][lr] == '2') return;
        if (grid[ud][lr] != '1') {

            return;
        }
        // do something

        grid[ud][lr] = '2';
        dfs(grid, ud + 1, lr);
        dfs(grid, ud - 1, lr);
        dfs(grid, ud, lr + 1);
        dfs(grid, ud, lr - 1);
    }

    private boolean overRange(char[][] grid, int ud, int lr) {
        return ud >= 0 && ud < grid.length && lr >= 0 && lr < grid[0].length;
    }


    private int area(char[][] grid, int r, int c) {
        return 1;
    }


    @Test
    public void test() {
        char[][] grid = {
                {'1', '1', '1', '1', '0'},
                {'1', '1', '0', '1', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '0', '0', '0'}
        };
        System.out.println(numIslands(grid));
        System.out.println(Arrays.deepToString(grid));

    }
}
